<?php

//<editor-fold defaultstate="collapsed" desc="License & Copyright">
/*
 * @license
 */

/**
 *
 * @author Jevgeni Geimanen (jevgeni.geimanen@gmail.com)
 */
//</editor-fold>

/**
 * @js-enum VALIDATOR_TYPE_ 
 */
abstract class SCFormFieldValidator extends SCStruct implements IJSStruct {

    const VALIDATOR_TYPE_BLANK = 'BLANK';
    const VALIDATOR_TYPE_REQUIRED = 'REQUIRED';

    public abstract function getValidatorType();

    public function __construct() {
        Site::getInstance()->RegisterComponent($this);
    }
    
    //<editor-fold defaultstate="collapsed" desc="ErrorMessage">
    /**
     * @var string
     */
    private $_ErrorMessage = null;

    /**
     *
     * @return string
     */
    public function getErrorMessage($error = null) {
        return $this->_ErrorMessage ? $this->_ErrorMessage : ($error ? $error : SCFormFieldValidator_Labels::ERROR_MESSAGE);
    }

    /**
     *
     * @param string $message
     * @return ErrorMessage 
     */
    public function setErrorMessage($message) {
        $this->_ErrorMessage = $message;
        return $this;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="FormField">
    /**
     * @var FormField
     */
    private $_FormField = null;

    /**
     *
     * @return FormField
     */
    public function getFormField() {
        return $this->_FormField;
    }

    /**
     *
     * @param FormField $field
     * @return FormField 
     */
    public function setFormField($field) {
        $this->_FormField = $field;
        return $this;
    }

    //</editor-fold>

    // <editor-fold defaultstate="collapsed" desc="IJSStruct">
    /**
     *
     * @var JSPropertiesCollection 
     */
    private $_JSProperties;
    
    /**
     *
     * @return array
     */
    public function getJSProperties() {
        if ($this->_JSProperties===null) {
            $this->_JSProperties = new JSPropertiesCollection();
            $this->_JSProperties->setStruct($this);
        }
        return $this->_JSProperties->getProperties();
    }
    
    /**
     *
     * @param JSPropertyCollection $collection 
     * @return Component
     */
    public function CollectJSProperties(&$collection) {
        $collection['error'] = sprintf($message = $this->getErrorMessage(), $this->getFormField()->getLabel());
        return $this;
    }
    // </editor-fold>
    
}

?>
